<?php

namespace app\admin\controller\union;
/* 用户邀请充值奖励流水 */

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\union\Guild;
use app\admin\model\union\StatDailyInviteReward;
use app\common\controller\Backend;

class Hostsharereward extends Backend
{
    protected $model = null;
    protected $searchFields = '';
    protected $admin_id = null;
    protected $guild_id = 0;
    protected $is_view = 0;
    protected $guild_manage_id = 0;
    public function _initialize()
    {
        parent::_initialize();
        //获取后台登录管理员id
        $auth = Auth::instance();
        $this->admin_id = $auth->isLogin() ? $auth->id : 0;
        $this->assign('admin_id', $this->admin_id);
        $this->guild_id = Admin::get(intval($auth->id))['guild_id'];
        if($this->guild_id){
            $this->is_view = Guild::where('id',$this->guild_id)->value('is_view');
        }
        $this->model = new StatDailyInviteReward();

    }

    public function index()
    {
        if($this->guild_id && !$this->is_view){
            echo "<div style='margin-top:300px;margin-left:500px;color: red'>如需查看旗下用户邀请充值奖励流水，请联系您的管理员开通渠道！</div>";die;
        }
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $field = "u.user_id,u.user_profile_id,u.nick_name,s.* ";
            $list = $this->model->field($field)
                ->alias('s')
                ->join('zy_voice_call.t_user_profile u',"u.user_id=s.user_id","LEFT")
                ->where($where);
            if($this->guild_id){ //超推登录
                $where2['g.guild_manage_id'] = $this->guild_id;
                $list = $list->join('zy_guild_manage.t_guild_host_manage g',"s.user_id=g.host_user_id","LEFT")->where($where2);
            }
            $list = $list->order($sort, $order)
                ->paginate($limit);
            foreach ($list->items() as $v){
                $v->reward_coin_rmb = floatval(round($v->reward_coin/10,2));
                $v->reward_coin = floatval($v->reward_coin);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
}